/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AlterarAlunoFrame.java
 *
 * Created on 19/11/2011, 20:35:06
 */
package br.com.bodylimit.fronteira;

import br.com.bodylimit.Main;
import br.com.bodylimit.model.Aluno;
import br.com.bodylimit.service.AlunoService;
import br.com.bodylimit.util.Mensagem;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Jõao Paulo
 */
public class AlterarAlunoFrame extends javax.swing.JFrame {

    Aluno aluno;
    Funcionalidade funcionalidade;

    /** Creates new form AlterarAlunoFrame */
    public AlterarAlunoFrame(Funcionalidade funcionalidade, Aluno aluno) {
        initComponents();
        this.aluno = aluno;
        this.funcionalidade = funcionalidade;
        if (funcionalidade == Funcionalidade.ADCIONAR) {
            preparaAdcionar();
            this.aluno = new Aluno();
            this.setTitle("Adcionar Aluno");
        } else if(funcionalidade == Funcionalidade.ALTERAR_REMOVER && aluno!=null && aluno.getMatricula() != 0) {
            preparaAlterarRemover();
            this.setTitle("Aluno: " + aluno.getNome() + " - Alterar ou Excluir");
        } else if(funcionalidade == Funcionalidade.DETALHAR && aluno !=null && aluno.getMatricula() != 0) {
            preparaDetalhar();
        } else if (aluno==null){
            Mensagem.msgErro("Aluno não selecionado.");
        } else {
            Mensagem.msgAviso("Função não esperada.");
        }
    }

    private void preparaAdcionar() {
        btnExcluir.setVisible(false);
        btnAlterar.setVisible(false);
        btnVerSerie.setEnabled(false);
        btnPagamentos.setEnabled(false);
        limparCampos();
    }

    private void preparaAlterarRemover() {
        btnExcluir.setVisible(true);
        btnAlterar.setVisible(true);
        btnAdcionar.setVisible(false);
        popularCampos();
    }

    private void preparaDetalhar() {
        txtMatricula.setEditable(false);
        txtNome.setEditable(false);
        txtDataNasc.setEditable(false);
        txtDiaVenc.setEditable(false);
        txtEndereco.setEditable(false);
        cmbSexo.setEnabled(false);

        btnAdcionar.setVisible(false);
        btnAlterar.setVisible(false);
        btnExcluir.setVisible(false);

        btnAdcionarTel.setEnabled(false);
        btnAlterarTel.setEnabled(false);
        btnExcluirTel.setEnabled(false);

        popularCampos();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtMatricula = new javax.swing.JTextField();
        txtNome = new javax.swing.JTextField();
        txtEndereco = new javax.swing.JTextField();
        cmbSexo = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        txtDataNasc = new javax.swing.JFormattedTextField();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstTelefone = new javax.swing.JList();
        btnAdcionarTel = new javax.swing.JButton();
        btnAlterarTel = new javax.swing.JButton();
        btnExcluirTel = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtDiaVenc = new javax.swing.JFormattedTextField();
        btnPagamentos = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        btnVerSerie = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        btnExcluir = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        btnAdcionar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Aluno"));
        jPanel1.setName("Dados do aluno: "); // NOI18N

        jLabel1.setText("Nome: ");

        jLabel2.setText("Matricula: ");

        jLabel3.setText("Endereco: ");

        jLabel4.setText("Sexo: ");

        txtMatricula.setEditable(false);

        cmbSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Masculino", "Feminino", " " }));

        jLabel5.setText("Data Nascimento: dd/MM/aaaa");

        txtDataNasc.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Telefones"));

        lstTelefone.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(lstTelefone);

        btnAdcionarTel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/bodylimit/images/BtnMais16.png"))); // NOI18N

        btnAlterarTel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/bodylimit/images/BtnRefresh16.png"))); // NOI18N

        btnExcluirTel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/bodylimit/images/BtnX.png"))); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnExcluirTel, 0, 0, Short.MAX_VALUE)
                    .addComponent(btnAdcionarTel, 0, 0, Short.MAX_VALUE)
                    .addComponent(btnAlterarTel, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(btnAdcionarTel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAlterarTel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnExcluirTel)))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cmbSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtDataNasc, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
                            .addComponent(txtEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
                            .addComponent(txtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)))
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtMatricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cmbSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtDataNasc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados de Pagamento"));

        jLabel6.setText("Dia de Vencimento: ");

        txtDiaVenc.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        btnPagamentos.setText("Vizualizar Pagamentos");
        btnPagamentos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPagamentosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDiaVenc, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(325, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(328, Short.MAX_VALUE)
                .addComponent(btnPagamentos)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtDiaVenc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnPagamentos)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Série"));

        btnVerSerie.setText("Ver Serie");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(402, Short.MAX_VALUE)
                .addComponent(btnVerSerie)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnVerSerie)
                .addContainerGap())
        );

        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/bodylimit/images/BtnX32.png"))); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/bodylimit/images/BtnRefresh32.png"))); // NOI18N
        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnAdcionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/bodylimit/images/BtnMais.png"))); // NOI18N
        btnAdcionar.setText("Adcionar");
        btnAdcionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdcionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAdcionar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 146, Short.MAX_VALUE)
                .addComponent(btnAlterar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnExcluir)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExcluir)
                    .addComponent(btnAlterar)
                    .addComponent(btnAdcionar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Limpa os campos do formulario
     */
    public void limparCampos() {
        txtMatricula.setText("");
        txtNome.setText("");
        txtDataNasc.setText("");
        txtDiaVenc.setText("");
        txtEndereco.setText("");
        lstTelefone.setListData(new Object[]{""});
    }

    /**
     * Preenche os campos do formulario com os dados da variavel aluno
     */
    private void popularCampos() {
        txtMatricula.setText(""+aluno.getMatricula());
        txtNome.setText(aluno.getNome());
        txtDataNasc.setText(new SimpleDateFormat("dd/MM/yyyy").format(aluno.getDataNascimento()));
        txtDiaVenc.setText(""+aluno.getDiaVencimento());
        txtEndereco.setText(aluno.getEndereco());
        if(aluno.getSexo()== 'M') {
            cmbSexo.getModel().setSelectedItem("Masculino");
        } else {
            cmbSexo.getModel().setSelectedItem("Feminino");
        }
        lstTelefone.setListData(aluno.getTelefones().toArray());
    }

    /**
     * Le e valida os campos do formulario retornando um aluno com os dados. Os telefones não estão presentes no aluno.
     * @return Um aluno com os dados de acordo com os que estão no formulario. null caso o aluno não seja validado corretamente.
     */
    private Aluno lerCampos() {
        Aluno lido = new Aluno();

        String matriculaStr = txtMatricula.getText();

        String nome = txtNome.getText();
        String end = txtEndereco.getText();
        String dtaNasc = txtDataNasc.getText();
        Date data = null;
        String diaVencStr = txtDiaVenc.getText();
        int diaVenc;
        String sexo = (String) cmbSexo.getModel().getSelectedItem();

        //Lendo matricula
        if(matriculaStr != null && !matriculaStr.equals("")) {
            try {
                lido.setMatricula(Integer.parseInt(matriculaStr));
                if(lido.getMatricula() != 0 && lido.getMatricula() != aluno.getMatricula()) {
                    Mensagem.msgAviso("A matricula do aluno não pode ser alterada");
                }
            } catch(NumberFormatException ex) {
                Mensagem.msgAviso("O campo matricula só aceita numeros.");
                return null;
            }
        }


        //Lendo nome
        if(nome==null || nome.equals("")) {
            Mensagem.msgAviso("Campo nome nao pode estar em branco.");
            return null;
        }
        lido.setNome(nome);

        //Lendo endereco
        if(end==null || end.equals("")) {
            Mensagem.msgAviso("Campo endereco nao pode estar em branco.");
            return null;
        }
        lido.setEndereco(end);

        //Lendo data de nascimento
        if(dtaNasc==null || dtaNasc.equals("")) {
            Mensagem.msgAviso("Data de nascimento nao pode estar em branco.");
            return null;
        }
        try {
            data = new SimpleDateFormat("dd/MM/yyyy").parse(dtaNasc);
        } catch(ParseException ex) {
            Mensagem.msgAviso("Formato incorreto de data.");
            return null;
        }
        lido.setDataNascimento(data);

        //Lendo dia de vencimento
        if(diaVencStr==null || diaVencStr.equals("")) {
            Mensagem.msgAviso("Campo dia do vencimento nao pode estar em branco.");
            return null;
        }
        try {
            diaVenc = Integer.parseInt(diaVencStr);
            if(diaVenc < 1 || diaVenc > 28) {
                Mensagem.msgAviso("O dia do vencimento deve ser entre 1 e 28.");
                return null;
            }
        } catch(NumberFormatException ex) {
            Mensagem.msgAviso("O campo dia de vencimento só aceita numeros.");
            return null;
        }
        lido.setEndereco(end);

        //Lendo Sexo
        if(sexo==null || sexo.equals("")) {
            Mensagem.msgAviso("Campo sexo nao pode estar em branco.");
            return null;
        }
        if(sexo.equals("Masculino"))
            lido.setSexo('M');
        else if(sexo.equals("Feminino"))
            lido.setSexo('F');
        else
            Mensagem.msgAviso("Sexo incorreto.");

        return lido;
    }

    private void btnPagamentosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPagamentosActionPerformed
    }//GEN-LAST:event_btnPagamentosActionPerformed

    private void btnAdcionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdcionarActionPerformed
        
        Aluno alunoLido = lerCampos();
        if(alunoLido!= null) {
            alunoLido.setTelefones(aluno.getTelefones());
            if(new AlunoService().adcionar(alunoLido)) {
                Mensagem.msgInfo("Aluno adcionado com sucesso!\nMatricula: " + alunoLido.getMatricula() + "\nNome: " + alunoLido.getNome());
                this.dispose();
            }
        }

    }//GEN-LAST:event_btnAdcionarActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        Aluno alunoLido = lerCampos();
        if(alunoLido!= null && alunoLido.getMatricula() != 0) {
            if(new AlunoService().atualizar(alunoLido)) {
                Mensagem.msgInfo("Dados do aluno alterados com sucesso!\nMatricula: " + alunoLido.getMatricula() + "\nNome: " + alunoLido.getNome());
            }
        }
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        Aluno alunoLido = lerCampos();
        if(alunoLido!= null && alunoLido.getMatricula() != 0) {
            int opcao = JOptionPane.showConfirmDialog(this, "Tem certeza que deseja excluir este aluno? Todos os dados de pagamentos e "
                    + "séries de exercicios serão perdidos. O processo é irreversivel!", "Confirmação de exclusão", JOptionPane.YES_NO_OPTION);
            if(opcao == JOptionPane.YES_OPTION) {
                if(new AlunoService().remover(alunoLido.getMatricula())) {
                    Mensagem.msgInfo("Aluno excluido com sucesso!\nMatricula: " + alunoLido.getMatricula() + "\nNome: " + alunoLido.getNome());
                    Main.getAlunoFrame().atualizarTabela();
                    this.dispose();
                }
            }
        }
    }//GEN-LAST:event_btnExcluirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdcionar;
    private javax.swing.JButton btnAdcionarTel;
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnAlterarTel;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnExcluirTel;
    private javax.swing.JButton btnPagamentos;
    private javax.swing.JButton btnVerSerie;
    private javax.swing.JComboBox cmbSexo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstTelefone;
    private javax.swing.JFormattedTextField txtDataNasc;
    private javax.swing.JFormattedTextField txtDiaVenc;
    private javax.swing.JTextField txtEndereco;
    private javax.swing.JTextField txtMatricula;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables
}
